package com.example.rocketmq.base.provider;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.util.concurrent.TimeUnit;

/**
 * 发送同步消息
 * 同步消息可靠性强，用于重要的消息通知
 * 
 *  1.创建消息生产者，并指定组
 *  2.指定nameserver地址
 *  3.启动生产者
 *  4.创建消息对象，topic tag和消息体
 *  5.发送消息，关闭生产者
 */
public class SyncProducer {
    public static void main(String[] args) throws Exception {
        DefaultMQProducer producer = new DefaultMQProducer("test_group");
        producer.setNamesrvAddr("192.168.228.101:9876;192.168.228.103:9876");
        producer.start();
        for (int i=0; i<10; i++) {
            Message msg = new Message("base", "tag1", String.format("hello world %s", i).getBytes());
            SendResult result = producer.send(msg);
            System.out.println(result);
            TimeUnit.SECONDS.sleep(1);
        }
        producer.shutdown();
    }
}
